#include "stdafx.h"

void hc::sleep(int ms)
{
	boost::xtime time;
	boost::xtime_get(&time, boost::TIME_UTC);
	time.nsec += ms*1000000;

	boost::thread().sleep(time);
}

using namespace hc;

TEvent::TEvent(bool autoReset):m_auto(autoReset),m_state(false)
{}
TEvent::~TEvent()
{}

void TEvent::Set()
{
	TLock _(m_mtx);
	m_state = true;
	m_cnd.notify_all();
}
void TEvent::Reset()
{
	TLock _(m_mtx);
	m_state = false;
	m_cnd.notify_all();
}

void TEvent::Wait()
{
	TLock _(m_mtx);
	while(!m_state)
	{
		m_cnd.wait(_);
	}
	if (m_auto)
		m_state = false;
}

bool TEvent::Wait(long milliseconds)
{
	TLock _(m_mtx);
	bool feedback;
	boost::xtime time;
	boost::xtime_get(&time, boost::TIME_UTC);
	time.nsec += milliseconds;
	while (!m_state) 
	{
		feedback = m_cnd.timed_wait(_, time);
	}
	if (feedback == true && m_auto) 
		m_state = false;
	return feedback;
}
